const { app, BrowserWindow, Menu,Tray,dialog, ipcMain} = require('electron');
const path = require('path');
const fs = require('fs');
let mainWindow;
const isMac = process.platform === 'darwin'

//创建菜单
const templateMenu = [
    {
        label: "文件",
        submenu: [
            {
                label: "新建"
            },
            {
                label: "新建窗口"
            },
            { type: 'separator' }, //分割线
            {
                label: "打开",
                click: function () {
                  
                  const res=  dialog.showOpenDialogSync({
                        title:"打开文件",
                        properties:["openFile"]
                    })
                    console.log(res);
                    if(res){
                        const filename=res[0];
                        const data = fs.readFileSync(filename, 'utf8');
                        console.log("读取到的文件内容是");
                        console.log(data);

                        mainWindow.webContents.send("file-opened",data);



                    }

                }
            },
            {
                label: "保存"
            },
            {
                label: "主进程给渲染进程发消息",
                click(){
                    mainWindow.webContents.send('message', 'hello world')
                }
            },
            { type: 'separator' }, //分割线
            {
                label: "退出",
                // role: isMac ? "close" : "quit",
                click: function () {
                    app.quit();
                }

            }
        ]


    },
    {
        label: "编辑"
    },
    {
        label: "视图"
    }
]
//创建菜单项目
const menu = Menu.buildFromTemplate(templateMenu);
Menu.setApplicationMenu(menu);

// Menu.setApplicationMenu(null); //去掉系统菜单


//创建托盘
let tray = null;
const initTray=()=>{
    const trayMenu=Menu.buildFromTemplate([
        {
            label:"打开",
            click:()=>{
                mainWindow.show();
            }
        },
        {
            label:"退出",
            click:()=>{
                app.quit();
            }
        }
    ]);

     tray = new Tray(path.join(__dirname, './icon/icon.ico'));
     tray.setToolTip("领云教务");
     tray.setContextMenu(trayMenu);
     tray.on("double-click",()=>{
        mainWindow.show();
     })

}


app.on("ready", () => {
    console.log('app is ready');

    initTray();
    createWindow();

})

// app.whenReady().then(()=>{
//     console.log('app is ready!!!');
// })

/**
 * 创建窗口
 */
const createWindow = () => {

    mainWindow = new BrowserWindow({
        width: 800,
        height: 600,
        // maximizable:false,
        // minimizable:false,
        // minWidth:400,
        // maxWidth:1000,
        // fullscreen:true
        // alwaysOnTop:true,
        // frame:false
        // show:false
        webPreferences: {      
            contextIsolation:false,
            nodeIntegration:true  
          }

    });

    //窗口加载网页
    // mainWindow.loadURL("http://www.baidu.com");
    mainWindow.loadFile('./html/index.html');

    mainWindow.on("close", (e) => {
        console.log("close window");
        // mainWindow.minimize();
        mainWindow.hide();
        //  setTimeout(()=>{
        //     mainWindow.show();
        // },3000);
        e.preventDefault();//阻止默认行为
    })


    mainWindow.on("blur", () => {
        console.log("blur window");
    })

    mainWindow.on("focus", () => {
        console.log("focus window");
    })

    mainWindow.on("show", () => {
        console.log("show window");
    })

    mainWindow.on("hide", () => {
        console.log("hide window");
    })

    // 打开开发者工具
    mainWindow.webContents.openDevTools();


}

app.on("quit", () => {

    console.log('app is quit');
})


ipcMain.on("close-window",(e,data)=>{

    console.log(e);
    console.log("接收到的数据",data);
    e.returnValue="哈尔滨火腿";

    e.reply("huili","腊肠");

    //app.quit();
})

// setTimeout(()=>{
//     app.quit();
// },5000)